package com.example.sunmiv1.scan;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;

import android.os.Handler;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;

import com.example.sunmiv1.R;
import com.example.sunmiv1.utils.zxing.Constant;

import net.nyx.printerservice.print.DeviceServiceChannel;
import net.nyx.printerservice.print.MemoryCacheManager;


public class InfraredScanActivity extends AppCompatActivity {


    TextView scanResultTextView;
    Handler handler;

    private int scannerType = 0;//報損/盤點/採購/採購退貨/出庫    1/2/3/4/5

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //EdgeToEdge.enable(this);
        setContentView(R.layout.activity_infrared_scan);
        findViewById(R.id.iv_left_back).setOnClickListener(v -> {
            finish();
        });
        scanResultTextView = findViewById(R.id.tv_scan_result);
        handler = new Handler();
        scannerType = getIntent().getIntExtra("scannerType", 0);
        findViewById(R.id.tv_press_start);
        findViewById(R.id.tv_scan_result_tip);

    }


    @Override
    protected void onStart() {
        super.onStart();
        //1.註冊廣播， 2.manifest中權限QUERY_ALL_PACKAGES 和 queries
        registerQscScanReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startScanBarcode();
    }


    void startScanBarcode() {
        DeviceServiceChannel.getInstance().triggerQscScan();
        handler.postDelayed(startScan, 6000);
    }

    Runnable startScan = () -> {
        DeviceServiceChannel.getInstance().triggerQscScan();
    };

    private final BroadcastReceiver qscReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (QR_ACTION.equals(intent.getAction())) {
                String qsc = intent.getStringExtra("qsc");
                if (!hasScan) {
                    hasScan = true;
                    scanResultTask(qsc);
                }
            }
        }
    };

    boolean hasScan = false;


    private static final String QR_ACTION = "com.android.NYX_QSC_DATA";

    void scanResultTask(String scanResult) {
        unregisterQscReceiver();
        scanResultTextView.setText(scanResult);
        MemoryCacheManager.getInstance().setScanData(scanResult);
        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.INTENT_EXTRA_KEY_QR_SCAN, scanResult);
        resultIntent.putExtras(bundle);
        //d setResult(int resultCode, Intent data)
        setResult(RESULT_OK, resultIntent);
        finish();
    }

    private void registerQscScanReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(QR_ACTION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            registerReceiver(qscReceiver, filter, Context.RECEIVER_VISIBLE_TO_INSTANT_APPS);
        } else {
            registerReceiver(qscReceiver, filter);
        }
    }

    private void unregisterQscReceiver() {
        handler.removeCallbacks(startScan);
        unregisterReceiver(qscReceiver);
    }


    @Override
    protected void onStop() {
        super.onStop();
       // unregisterQscReceiver();
    }
}
